from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.alert import Alert

# 设置正确的驱动路径
service = ChromeService(executable_path="D:\Downloads\chromedriver-win64\chromedriver-win64\chromedriver.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)


# 打开网页
driver.get("https://example.com")

# 触发 alert 弹窗
driver.execute_script("alert('这是一个 alert 弹窗');")

# 切换到 alert 弹窗
alert = Alert(driver)

# 获取弹窗文本
print(alert.text)

# 点击"确定"按钮关闭弹窗
alert.accept()

# 关闭浏览器
driver.quit()